/*
 * OknoTest.java
 *
 * Created on 19 maj 2009, 15:15
 */

package BasaSobol;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author  bąsa
 */
public class OknoTest extends javax.swing.JFrame implements ActionListener {
    
    /** Creates new form OknoTest */
    public OknoTest() {
        initComponents();
        testWyslij.addActionListener(this);
        testDodaj.addActionListener(this);
        testUsun.addActionListener(this);
        testStatus.addActionListener(this);
        testACK.addActionListener(this);
        testREG.addActionListener(this);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        testIP1 = new javax.swing.JTextField();
        testNick = new javax.swing.JTextField();
        testDodaj = new javax.swing.JButton();
        testIP2 = new javax.swing.JTextField();
        testWiadomosc = new javax.swing.JTextField();
        testWyslij = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        testUsun = new javax.swing.JButton();
        testStatus = new javax.swing.JButton();
        testACK = new javax.swing.JButton();
        testREG = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        testIP1.setText("127.0.0.1");

        testNick.setText("Kasia");

        testDodaj.setText("Dodaj");

        testIP2.setText("127.0.0.1");

        testWiadomosc.setText("asdfdsaf");

        testWyslij.setText("Wyslij");

        jLabel1.setText("ip");

        jLabel2.setText("klient");

        jLabel3.setText("ip");

        jLabel4.setText("wiadomosc");

        testUsun.setText("Usun");

        testStatus.setText("Status");

        testACK.setText("ACK");

        testREG.setText("REG");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(testIP1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(testNick, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(testDodaj)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(testUsun)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(testStatus))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(testIP2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(testWiadomosc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel4))
                    .addComponent(testWyslij)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(testACK)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(testREG)))
                .addContainerGap(191, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(testIP1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(testNick, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(testDodaj)
                    .addComponent(testUsun)
                    .addComponent(testStatus))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(testIP2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(testWiadomosc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(testWyslij)
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(testACK)
                    .addComponent(testREG))
                .addContainerGap(84, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new OknoTest().setVisible(true);
            }
        });
    }
    
    private void dodajKlienta() {
        tinsiec.MessageInfo msg = new tinsiec.MessageInfo(modulXML.wiadomoscONowymKliencie(testNick.getText()), testIP1.getText());
        System.out.println(msg.getContent());
        Main.wiadomosciPrzychodzaceMulticast.add(msg);
    }
    
    private void usunKlienta() {
        tinsiec.MessageInfo msg = new tinsiec.MessageInfo(modulXML.wiadomoscOUsuwanymKliencie(testNick.getText()), testIP1.getText());
        System.out.println(msg.getContent());
        Main.wiadomosciPrzychodzaceMulticast.add(msg);
    }
    
    private void wyslijWiadomosc() {
        tinsiec.Message msg = new tinsiec.Message(testIP2.getText(), modulXML.wiadomoscDoWyslania(testWiadomosc.getText()));
        System.out.println(msg.getMessage());
        Main.wiadomosciPrzychodzace.add(msg);
    }
    
    private void zmienStatus() {
        tinsiec.MessageInfo msg = new tinsiec.MessageInfo(modulXML.wiadomoscZmianaStatusu(testNick.getText()), testIP1.getText());
        System.out.println(msg.getContent());
        Main.wiadomosciPrzychodzaceMulticast.add(msg);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JButton testACK;
    private javax.swing.JButton testDodaj;
    private javax.swing.JTextField testIP1;
    private javax.swing.JTextField testIP2;
    private javax.swing.JTextField testNick;
    private javax.swing.JButton testREG;
    private javax.swing.JButton testStatus;
    private javax.swing.JButton testUsun;
    private javax.swing.JTextField testWiadomosc;
    private javax.swing.JButton testWyslij;
    // End of variables declaration//GEN-END:variables

    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("Dodaj")) {
            dodajKlienta();
        }
        
        if(e.getActionCommand().equals("Usun")) {
            usunKlienta();
        }
        
        if(e.getActionCommand().equals("Wyslij")) {
            wyslijWiadomosc();
        }
        
        if(e.getActionCommand().equals("Status")) {
            zmienStatus();
        }
        
        if(e.getActionCommand().equals("ACK")) {
            tinsiec.Message msg = new tinsiec.Message("127.0.0.1", modulXML.wiadomoscACK("Sobol", "Zaraz wracam"));
            Main.wiadomosciPrzychodzace.add(msg);
            System.out.println(msg.getMessage());
        }
        
        if(e.getActionCommand().equals("REG")) {
            tinsiec.Message msg = new tinsiec.Message("127.0.0.1", modulXML.wiadomoscREG());
            Main.wiadomosciPrzychodzace.add(msg);
            System.out.println(msg.getMessage());
        }
    }
    
}
